package 剑指Offer.medium;

public class 构建乘积数组 {
    class Solution {
        public int[] constructArr(int[] a) {
            if (a == null || a.length == 0) return new int[0];
            int[] left = new int[a.length];
            int[] right = new int[a.length];
            int[] res = new int[a.length];

            left[0] = right[right.length - 1] = 1;

            for (int i = 1; i < left.length; i++) {
                left[i] = a[i - 1] * left[i - 1];
            }

            for (int i = right.length - 2; i >= 0; i--) {
                right[i] = a[i + 1] * right[i + 1];
            }

            for (int i = 0; i < res.length; i++) {
                res[i] = left[i] * right[i];
            }

            return res;
        }
    }
}
